package scau.gsmg.misp.action;


import java.util.Map;

import javax.security.auth.login.AccountLockedException;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import scau.gsmg.misp.vo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @Title: LoginAction.java
 * @Group: GaoShenMenGui
 * @Author: WXY
 * @Date: 2015-5-31
 */
@ParentPackage("json-default")
@Scope("prototype")
@Namespace(value = "/")

@Results({
	@Result(name="loginSuccess",type="json",params={"root","jsonObject"}),
	@Result(name="logout", type="redirect", location="/index.jsp")})
@Controller
public class LoginAction extends ActionSupport{

	HttpServletRequest request = ServletActionContext.getRequest();
	HttpServletResponse response = ServletActionContext.getResponse();
	
	private static final long serialVersionUID = 1L;
	/*private String username;
	private String password;
	private String rememberMe;*/
	private JSONObject jsonObject;
//	private List<User> userList;
//	private String errorException;
	

	/**
	 * @Description:用户登陆
	 */
	@Action(value="login")
	public String login() {
		
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        String errorException = null;
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {//已认证且用户手动打开登录页
        	Map session =ActionContext.getContext().getSession();
//        	Map request =(Map)ActionContext.getContext().getContext().get("request");
        	User user=(User) session.get("user");
//        	request.put("user", "kk");
//        	HttpServletRequest request = ServletActionContext.getRequest();
//        	  HttpSession session = request.getSession();
//        	  session.setAttribute("user", user);
        	JSONObject json=new JSONObject();
        	json.put("success", true);
        	json.put("user", user.getName());
    		jsonObject=json;
        	
        } else {
            if ("scau.gsmg.misp.utils.IncorrectCaptchaException".equals(exceptionClassName)) {
                errorException = "验证码错误！";
            } else if ("org.apache.shiro.authc.UnknownAccountException".equals(exceptionClassName)) {
                errorException = "用户名不存在！";
            }else if ("org.apache.shiro.authc.IncorrectCredentialsException".equals(exceptionClassName)) {
            	errorException = "密码错误！";
            }
            else if (AccountLockedException.class.getSimpleName().equals(exceptionClassName)) {
                errorException = "帐号已经锁定";
            }else{
                errorException = "其他错误！请稍后重试！";
            }
        	JSONObject json=new JSONObject();
        	json.put("success",errorException);
    		jsonObject=json;
        }
        System.out.println(errorException);
        return "loginSuccess";
	}

	/**
	 * @Description:登出
	 */
	@Action(value="logout")
	public String logout() {
//		ShiroUtil.getSession().removeAttribute("user");
//		HttpServletRequest request = ServletActionContext.getRequest();
//		HttpSession sessionUser = request.getSession();
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
//		sessionUser.invalidate();//
		System.out.println("loginout");
		return "logout";
	}

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
}
